package org.kaer.election.utils;

import android.content.Context;
import android.os.Environment;
import android.util.Log;

import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author Lzj (KAER)
 * @Create 2015年7月10日 下午3:30:28
 * @Version 1
 * @Description
 */
public class LogUtils {

    public static final int LEVEL_MAX = 100;
    public static final int LEVEL_MIN = 0;
    private static String TAG = "com.kaer.sdk";
    private static boolean DEBUG = true;
    public static boolean SHOW_MORE_INFOS = false;
    public static String ERROR_MESSAGE = "程序出现异常,我们会尽快修复!";
    private static String fileName;
    private static SimpleDateFormat format = new SimpleDateFormat(
            "yyyy-MM-dd HH:mm:ss SSS");

    public static void init(Context context) {
        //TAG = context.getPackageName();
        SHOW_MORE_INFOS = false;
        getFileName(context);

    }

    private static void getFileName(Context context) {
        File sdDir = null;
        boolean sdCardExist = Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED); // 判断sd卡是否存在
        if (sdCardExist) {
            LogUtils.i("存在sdCard");
            sdDir = Environment.getExternalStorageDirectory();// 获取跟目录
            fileName = sdDir.toString() + "/" + "kaerlog.txt";
            deleteFile();
        } else {
            LogUtils.e("无sdCard");

        }

    }

    private static void writeFile(String data) {
        try {

            FileOutputStream fout = new FileOutputStream(fileName, SHOW_MORE_INFOS);
            byte[] bytes = data.getBytes();
            fout.write(bytes);
            fout.close();
            if (!SHOW_MORE_INFOS) {
                SHOW_MORE_INFOS = true;
            }
        } catch (Exception e) {
            // LogUtils.e("LogUtils "+e.getMessage());
        }
    }

    public static void deleteFile() {
        SHOW_MORE_INFOS = false;
        File file = new File(fileName);
        if (file.exists()) { // 判断文件是否存在
            if (file.isFile()) { // 判断是否是文件
                file.delete(); // delete()方法 你应该知道 是删除的意思;

            } else {
            }
        }
    }

    /**
     * 设置crash提示信息
     *
     * @param Message
     */
    public static void setMessage(String Message) {
        ERROR_MESSAGE = Message;
    }

    /**
     * 设置是否显示详细的crash信息
     *
     * @param show
     */
    public static void showMoreInfos(boolean show) {
        SHOW_MORE_INFOS = show;
    }

    public static void v(String tag, String msg, int level) {
        if (DEBUG || level > 0) {
            Log.v(tag, msg);
        }
        //   wirteFile(format.format(new Date()) + ":" + msg + "\n");

    }

    public static void d(String tag, String msg, int level) {
        if (DEBUG || level > 0) {
            Log.d(tag, msg);
        }
        //  wirteFile(format.format(new Date()) + ":" + msg + "\n");

    }

    public static void i(String tag, String msg, int level) {
        if (DEBUG || level > 0) {
            Log.i(tag, msg);
        }
        writeFile(format.format(new Date()) + ":" + msg + "\n");

    }

    public static void w(String tag, String msg, int level) {
        if (DEBUG || level > 0) {
            Log.w(tag, msg);
        }
        writeFile(format.format(new Date()) + ":" + msg + "\n");

    }

    public static void e(String tag, String msg, int level) {
        if (DEBUG || level > 0) {
            Log.e(tag, msg);
        }
        writeFile(format.format(new Date()) + ":" + msg + "\n");

    }

    public static void v(String msg) {
        v(TAG, msg, LEVEL_MIN);

    }

    public static void d(String msg) {
        d(TAG, msg, LEVEL_MIN);

    }

    public static void i(String msg) {
        i(TAG, msg, LEVEL_MIN);

    }

    public static void w(String msg) {
        w(TAG, msg, LEVEL_MIN);

    }

    public static void e(String msg) {
        e(TAG, msg, LEVEL_MIN);

    }

    public static void v(String msg, int level) {
        v(TAG, msg, level);

    }

    public static void d(String msg, int level) {
        d(TAG, msg, level);

    }

    public static void i(String msg, int level) {
        i(TAG, msg, level);

    }

    public static void w(String msg, int level) {
        w(TAG, msg, level);

    }

    public static void e(String msg, int level) {
        e(TAG, msg, level);

    }

    public static String getTAG() {
        return TAG;
    }

    public static void setTAG(String tAG) {
        TAG = tAG;
    }

    public static boolean isDebug() {
        return DEBUG;
    }

    public static void setDebug(boolean Debug) {
        DEBUG = Debug;
    }

  
}
